#!/usr/bin/perl
# Count number of WWW events per day
use LogCount;

$linecount = 5000;
$usage = "Usage: $0 [-lines count]\n";
if ($ENV{COUNTER_HOST}) {
    $counterserver = $ENV{COUNTER_HOST};
} else {
    $counterserver = "counter.li.org";
}

@fields = qw(total GET POST html php image cgi);
  
while ($ARGV[0] =~ /^-./) {
    $opt = shift;
    if ($opt eq "-days") {
	$daycount = shift;
    } elsif ($opt eq "-byhour") {
	$byhour = 1;
    } elsif ($opt eq "-to") {
	$todate = shift;
    } else {
	die $usage;
    }
}
if ($byhour && $daycount) {
    $daycount *= 24; # 1 day = 24 hours; external beauty, not internal...
}
while (<>) {
    chop;
    if (/\[([^\]]+)\] (\S+)? ?"/) {
        $server = $2;
        $command = $';
        if (!$server) {
           ++ $oldformatlines;
           next;
        } elsif ($server ne $counterserver) {
           ++ $notcounterlines;
           next;
        }
        $date = LogCount::apachetosimple($1, $byhour);
        if ($date =~ /^0/ || $date !~ /^\d{4}-/) {
           ++ $dateerrors;
           next;
        }
	++ $count{$date}{total};
	if ($count{$date}{total} == 1) {
	    $seqno{$date} = $seqno;
	    ++ $seqno;
	}
	($cmd, $file) = split(" ", $command);
        ++ $count{$date}{$cmd};
        ++ $count{$date}{cgi} if ($file =~ /cgi-bin/);
        ++ $count{$date}{html} if ($file =~ /.html$/);
        ++ $count{$date}{php} if ($file =~ /.php$/);
        ++ $count{$date}{image} if ($file =~ /.gif$/ || $file =~ /.png$/);
    } else {
	print STDERR "Unparsed: ", asciify($_), "\n";
    }
}
print STDERR "$oldformatlines old format lines (no server)\n"
    if $oldformatlines;
print STDERR "$notcounterlines lines not marked with $counterserver\n"
    if $notcounterlines;

LogCount::zerofilltimes(\%count, $byhour);

printf "#%-13s", "Date";
for $field (@fields) {
    printf " %5.5s", $field;
}
print "\n";
@keys = sort(keys(%count));
$nkeys = $#keys;
if ($daycount) {
    $skip = $nkeys - $daycount;
    print STDERR "Skipping $skip of $nkeys output lines\n";
}


for $date (sort keys(%count)) {
    next if ($skip-- > 0);
#    next if $date lt "1990"; # Delete some rubbish dates
    last if $todate && $date ge $todate;
    printf "%10s ", $date;
    for $field (@fields) {
	printf " %5d", $count{$date}{$field};
    }
    print "\n";
}

sub asciify {
    my $str = shift;
    $str =~ tr/\000-\037//d;
    $str;
}
